package ffmpeg

/*
#cgo LDFLAGS: -lavformat -lavcodec

#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
*/
import "C"

import (
	"log"
	"reflect"
	"unsafe"
)

type AVStream C.AVStream

func init() {
	C.avcodec_register_all()
	C.av_register_all()
	if retCode := C.avformat_network_init(); retCode < 0 {
		log.Fatalf("Call to avformat_network_init failed: %d.", retCode)
	}
}

func getStreams(fmtCtx *C.AVFormatContext) []*AVStream {
	streamSliceHdr := reflect.SliceHeader{
		Data: uintptr(unsafe.Pointer(fmtCtx.streams)),
		Len:  int(fmtCtx.nb_streams),
		Cap:  int(fmtCtx.nb_streams),
	}
	return *(*[]*AVStream)(unsafe.Pointer(&streamSliceHdr))
}

func (s *AVStream) MediaType() string {
	return C.GoString(C.av_get_media_type_string(s.codec.codec_type))
}
